home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / libs / readline / doc / rltech.tex < prev    next >
Encoding:
Text File  |  1995-09-19  |  44.4 KB  |  1,407 lines

  1. @comment %**start of header (This is for running Texinfo on a region.)
  2. @setfilename rltech.info
  3. @comment %**end of header (This is for running Texinfo on a region.)
  4. @setchapternewpage odd
  5.  
  6. @ifinfo
  7. This document describes the GNU Readline Library, a utility for aiding
  8. in the consitency of user interface across discrete programs that need
  9. to provide a command line interface.
  10.  
  11. Copyright (C) 1988, 1994 Free Software Foundation, Inc.
  12.  
  13. Permission is granted to make and distribute verbatim copies of
  14. this manual provided the copyright notice and this permission notice
  15. pare preserved on all copies.
  16.  
  17. @ignore
  18. Permission is granted to process this file through TeX and print the
  19. results, provided the printed document carries copying permission
  20. notice identical to this one except for the removal of this paragraph
  21. (this paragraph not being relevant to the printed manual).
  22. @end ignore
  23.  
  24. Permission is granted to copy and distribute modified versions of this
  25. manual under the conditions for verbatim copying, provided that the entire
  26. resulting derived work is distributed under the terms of a permission
  27. notice identical to this one.
  28.  
  29. Permission is granted to copy and distribute translations of this manual
  30. into another language, under the above conditions for modified versions,
  31. except that this permission notice may be stated in a translation approved
  32. by the Foundation.
  33. @end ifinfo
  34.  
  35. @node Programming with GNU Readline
  36. @chapter Programming with GNU Readline
  37.  
  38. This chapter describes the interface between the GNU Readline Library and
  39. other programs.  If you are a programmer, and you wish to include the
  40. features found in GNU Readline
  41. such as completion, line editing, and interactive history manipulation
  42. in your own programs, this section is for you.
  43.  
  44. @menu
  45. * Basic Behavior::    Using the default behavior of Readline.
  46. * Custom Functions::    Adding your own functions to Readline.
  47. * Readline Variables::            Variables accessible to custom
  48.                     functions.
  49. * Readline Convenience Functions::    Functions which Readline supplies to
  50.                     aid in writing your own
  51. * Custom Completers::    Supplanting or supplementing Readline's
  52.             completion functions.
  53. @end menu
  54.  
  55. @node Basic Behavior
  56. @section Basic Behavior
  57.  
  58. Many programs provide a command line interface, such as @code{mail},
  59. @code{ftp}, and @code{sh}.  For such programs, the default behaviour of
  60. Readline is sufficient.  This section describes how to use Readline in
  61. the simplest way possible, perhaps to replace calls in your code to
  62. @code{gets()} or @code{fgets ()}.
  63.  
  64. @findex readline
  65. @cindex readline, function
  66. The function @code{readline ()} prints a prompt and then reads and returns
  67. a single line of text from the user.  The line @code{readline}
  68. returns is allocated with @code{malloc ()}; you should @code{free ()}
  69. the line when you are done with it.  The declaration for @code{readline}
  70. in ANSI C is
  71.  
  72. @example
  73. @code{char *readline (char *@var{prompt});}
  74. @end example
  75.  
  76. @noindent
  77. So, one might say
  78. @example
  79. @code{char *line = readline ("Enter a line: ");}
  80. @end example
  81. @noindent
  82. in order to read a line of text from the user.
  83. The line returned has the final newline removed, so only the
  84. text remains.
  85.  
  86. If @code{readline} encounters an @code{EOF} while reading the line, and the
  87. line is empty at that point, then @code{(char *)NULL} is returned.
  88. Otherwise, the line is ended just as if a newline had been typed.
  89.  
  90. If you want the user to be able to get at the line later, (with
  91. @key{C-p} for example), you must call @code{add_history ()} to save the
  92. line away in a @dfn{history} list of such lines.
  93.  
  94. @example
  95. @code{add_history (line)};
  96. @end example
  97.  
  98. @noindent
  99. For full details on the GNU History Library, see the associated manual.
  100.  
  101. It is preferable to avoid saving empty lines on the history list, since
  102. users rarely have a burning need to reuse a blank line.  Here is
  103. a function which usefully replaces the standard @code{gets ()} library
  104. function, and has the advantage of no static buffer to overflow:
  105.  
  106. @example
  107. /* A static variable for holding the line. */
  108. static char *line_read = (char *)NULL;
  109.  
  110. /* Read a string, and return a pointer to it.  Returns NULL on EOF. */
  111. char *
  112. rl_gets ()
  113. @{
  114.   /* If the buffer has already been allocated, return the memory
  115.      to the free pool. */
  116.   if (line_read)
  117.     @{
  118.       free (line_read);
  119.       line_read = (char *)NULL;
  120.     @}
  121.  
  122.   /* Get a line from the user. */
  123.   line_read = readline ("");
  124.  
  125.   /* If the line has any text in it, save it on the history. */
  126.   if (line_read && *line_read)
  127.     add_history (line_read);
  128.  
  129.   return (line_read);
  130. @}
  131. @end example
  132.  
  133. This function gives the user the default behaviour of @key{TAB}
  134. completion: completion on file names.  If you do not want Readline to
  135. complete on filenames, you can change the binding of the @key{TAB} key
  136. with @code{rl_bind_key ()}.
  137.  
  138. @example
  139. @code{int rl_bind_key (int @var{key}, int (*@var{function})());}
  140. @end example
  141.  
  142. @code{rl_bind_key ()} takes two arguments: @var{key} is the character that
  143. you want to bind, and @var{function} is the address of the function to
  144. call when @var{key} is pressed.  Binding @key{TAB} to @code{rl_insert ()}
  145. makes @key{TAB} insert itself.
  146. @code{rl_bind_key ()} returns non-zero if @var{key} is not a valid
  147. ASCII character code (between 0 and 255).
  148.  
  149. Thus, to disable the default @key{TAB} behavior, the following suffices:
  150. @example
  151. @code{rl_bind_key ('\t', rl_insert);}
  152. @end example
  153.  
  154. This code should be executed once at the start of your program; you
  155. might write a function called @code{initialize_readline ()} which
  156. performs this and other desired initializations, such as installing
  157. custom completers (@pxref{Custom Completers}).
  158.  
  159. @node Custom Functions
  160. @section Custom Functions
  161.  
  162. Readline provides many functions for manipulating the text of
  163. the line, but it isn't possible to anticipate the needs of all
  164. programs.  This section describes the various functions and variables
  165. defined within the Readline library which allow a user program to add
  166. customized functionality to Readline.
  167.  
  168. @menu
  169. * The Function Type::    C declarations to make code readable.
  170. * Function Writing::    Variables and calling conventions.
  171. @end menu
  172.  
  173. @node The Function Type
  174. @subsection The Function Type
  175.  
  176. For readabilty, we declare a new type of object, called
  177. @dfn{Function}.  A @code{Function} is a C function which
  178. returns an @code{int}.  The type declaration for @code{Function} is:
  179.  
  180. @noindent
  181. @code{typedef int Function ();}
  182.  
  183. The reason for declaring this new type is to make it easier to write
  184. code describing pointers to C functions.  Let us say we had a variable
  185. called @var{func} which was a pointer to a function.  Instead of the
  186. classic C declaration
  187.  
  188. @code{int (*)()func;}
  189.  
  190. @noindent
  191. we may write
  192.  
  193. @code{Function *func;}
  194.  
  195. @noindent
  196. Similarly, there are
  197.  
  198. @example
  199. typedef void VFunction ();
  200. typedef char *CPFunction (); @r{and}
  201. typedef char **CPPFunction ();
  202. @end example
  203.  
  204. @noindent
  205. for functions returning no value, @code{pointer to char}, and
  206. @code{pointer to pointer to char}, respectively.
  207.  
  208. @node Function Writing
  209. @subsection Writing a New Function
  210.  
  211. In order to write new functions for Readline, you need to know the
  212. calling conventions for keyboard-invoked functions, and the names of the
  213. variables that describe the current state of the line read so far.
  214.  
  215. The calling sequence for a command @code{foo} looks like
  216.  
  217. @example
  218. @code{foo (int count, int key)}
  219. @end example
  220.  
  221. @noindent
  222. where @var{count} is the numeric argument (or 1 if defaulted) and
  223. @var{key} is the key that invoked this function.
  224.  
  225. It is completely up to the function as to what should be done with the
  226. numeric argument.  Some functions use it as a repeat count, some
  227. as a flag, and others to choose alternate behavior (refreshing the current
  228. line as opposed to refreshing the screen, for example).  Some choose to
  229. ignore it.  In general, if a
  230. function uses the numeric argument as a repeat count, it should be able
  231. to do something useful with both negative and positive arguments.
  232. At the very least, it should be aware that it can be passed a
  233. negative argument.
  234.  
  235. @node Readline Variables
  236. @section Readline Variables
  237.  
  238. These variables are available to function writers.
  239.  
  240. @deftypevar {char *} rl_line_buffer
  241. This is the line gathered so far.  You are welcome to modify the
  242. contents of the line, but see @ref{Allowing Undoing}.
  243. @end deftypevar
  244.  
  245. @deftypevar int rl_point
  246. The offset of the current cursor position in @code{rl_line_buffer}
  247. (the @emph{point}).
  248. @end deftypevar
  249.  
  250. @deftypevar int rl_end
  251. The number of characters present in @code{rl_line_buffer}.  When
  252. @code{rl_point} is at the end of the line, @code{rl_point} and
  253. @code{rl_end} are equal.
  254. @end deftypevar
  255.  
  256. @deftypevar int rl_mark
  257. The mark (saved position) in the current line.  If set, the mark
  258. and point define a @emph{region}.
  259. @end deftypevar
  260.  
  261. @deftypevar int rl_done
  262. Setting this to a non-zero value causes Readline to return the current
  263. line immediately.
  264. @end deftypevar
  265.  
  266. @deftypevar int rl_pending_input
  267. Setting this to a value makes it the next keystroke read.  This is a
  268. way to stuff a single character into the input stream.
  269. @end deftypevar
  270.  
  271. @deftypevar {char *} rl_prompt
  272. The prompt Readline uses.  This is set from the argument to
  273. @code{readline ()}, and should not be assigned to directly.
  274. @end deftypevar
  275.  
  276. @deftypevar {char *} rl_terminal_name
  277. The terminal type, used for initialization.
  278. @end deftypevar
  279.  
  280. @deftypevar {char *} rl_readline_name
  281. This variable is set to a unique name by each application using Readline.
  282. The value allows conditional parsing of the inputrc file
  283. (@pxref{Conditional Init Constructs}).
  284. @end deftypevar
  285.  
  286. @deftypevar {FILE *} rl_instream
  287. The stdio stream from which Readline reads input.
  288. @end deftypevar
  289.  
  290. @deftypevar {FILE *} rl_outstream
  291. The stdio stream to which Readline performs output.
  292. @end deftypevar
  293.  
  294. @deftypevar {Function *} rl_startup_hook
  295. If non-zero, this is the address of a function to call just
  296. before @code{readline} prints the first prompt.
  297. @end deftypevar
  298.  
  299. @deftypevar {Function *} rl_event_hook
  300. If non-zero, this is the address of a function to call periodically
  301. when readline is waiting for terminal input.
  302. @end deftypevar
  303.  
  304. @node Readline Convenience Functions
  305. @section Readline Convenience Functions
  306.  
  307. @menu
  308. * Function Naming::    How to give a function you write a name.
  309. * Keymaps::        Making keymaps.
  310. * Binding Keys::    Changing Keymaps.
  311. * Associating Function Names and Bindings::    Translate function names to
  312.                         key sequences.
  313. * Allowing Undoing::    How to make your functions undoable.
  314. * Redisplay::        Functions to control line display.
  315. * Modifying Text::    Functions to modify @code{rl_line_buffer}.
  316. * Utility Functions::    Generally useful functions and hooks.
  317. @end menu
  318.  
  319. @node Function Naming
  320. @subsection Naming a Function
  321.  
  322. The user can dynamically change the bindings of keys while using
  323. Readline.  This is done by representing the function with a descriptive
  324. name.  The user is able to type the descriptive name when referring to
  325. the function.  Thus, in an init file, one might find
  326.  
  327. @example
  328. Meta-Rubout:    backward-kill-word
  329. @end example
  330.  
  331. This binds the keystroke @key{Meta-Rubout} to the function
  332. @emph{descriptively} named @code{backward-kill-word}.  You, as the
  333. programmer, should bind the functions you write to descriptive names as
  334. well.  Readline provides a function for doing that:
  335.  
  336. @deftypefun int rl_add_defun (char *name, Function *function, int key)
  337. Add @var{name} to the list of named functions.  Make @var{function} be
  338. the function that gets called.  If @var{key} is not -1, then bind it to
  339. @var{function} using @code{rl_bind_key ()}.
  340. @end deftypefun
  341.  
  342. Using this function alone is sufficient for most applications.  It is
  343. the recommended way to add a few functions to the default functions that
  344. Readline has built in.  If you need to do something other
  345. than adding a function to Readline, you may need to use the
  346. underlying functions described below.
  347.  
  348. @node Keymaps
  349. @subsection Selecting a Keymap
  350.  
  351. Key bindings take place on a @dfn{keymap}.  The keymap is the
  352. association between the keys that the user types and the functions that
  353. get run.  You can make your own keymaps, copy existing keymaps, and tell
  354. Readline which keymap to use.
  355.  
  356. @deftypefun Keymap rl_make_bare_keymap ()
  357. Returns a new, empty keymap.  The space for the keymap is allocated with
  358. @code{malloc ()}; you should @code{free ()} it when you are done.
  359. @end deftypefun
  360.  
  361. @deftypefun Keymap rl_copy_keymap (Keymap map)
  362. Return a new keymap which is a copy of @var{map}.
  363. @end deftypefun
  364.  
  365. @deftypefun Keymap rl_make_keymap ()
  366. Return a new keymap with the printing characters bound to rl_insert,
  367. the lowercase Meta characters bound to run their equivalents, and
  368. the Meta digits bound to produce numeric arguments.
  369. @end deftypefun
  370.  
  371. @deftypefun void rl_discard_keymap (Keymap keymap)
  372. Free the storage associated with @var{keymap}.
  373. @end deftypefun
  374.  
  375. Readline has several internal keymaps.  These functions allow you to
  376. change which keymap is active.
  377.  
  378. @deftypefun Keymap rl_get_keymap ()
  379. Returns the currently active keymap.
  380. @end deftypefun
  381.  
  382. @deftypefun void rl_set_keymap (Keymap keymap)
  383. Makes @var{keymap} the currently active keymap.
  384. @end deftypefun
  385.  
  386. @deftypefun Keymap rl_get_keymap_by_name (char *name)
  387. Return the keymap matching @var{name}.  @var{name} is one which would
  388. be supplied in a @code{set keymap} inputrc line (@pxref{Readline Init File}).
  389. @end deftypefun
  390.  
  391. @node Binding Keys
  392. @subsection Binding Keys
  393.  
  394. You associate keys with functions through the keymap.  Readline has
  395. several internal keymaps: @code{emacs_standard_keymap},
  396. @code{emacs_meta_keymap}, @code{emacs_ctlx_keymap},
  397. @code{vi_movement_keymap}, and @code{vi_insertion_keymap}.
  398. @code{emacs_standard_keymap} is the default, and the examples in
  399. this manual assume that.
  400.  
  401. These functions manage key bindings.
  402.  
  403. @deftypefun int rl_bind_key (int key, Function *function)
  404. Binds @var{key} to @var{function} in the currently active keymap.
  405. Returns non-zero in the case of an invalid @var{key}.
  406. @end deftypefun
  407.  
  408. @deftypefun int rl_bind_key_in_map (int key, Function *function, Keymap map)
  409. Bind @var{key} to @var{function} in @var{map}.  Returns non-zero in the case
  410. of an invalid @var{key}.
  411. @end deftypefun
  412.  
  413. @deftypefun int rl_unbind_key (int key)
  414. Bind @var{key} to the null function in the currently active keymap.
  415. Returns non-zero in case of error.
  416. @end deftypefun
  417.  
  418. @deftypefun int rl_unbind_key_in_map (int key, Keymap map)
  419. Bind @var{key} to the null function in @var{map}.
  420. Returns non-zero in case of error.
  421. @end deftypefun
  422.  
  423. @deftypefun int rl_generic_bind (int type, char *keyseq, char *data, Keymap map)
  424. Bind the key sequence represented by the string @var{keyseq} to the arbitrary
  425. pointer @var{data}.  @var{type} says what kind of data is pointed to by
  426. @var{data}; this can be a function (@code{ISFUNC}), a macro
  427. (@code{ISMACR}), or a keymap (@code{ISKMAP}).  This makes new keymaps as
  428. necessary.  The initial keymap in which to do bindings is @var{map}.
  429. @end deftypefun
  430.  
  431. @deftypefun int rl_parse_and_bind (char *line)
  432. Parse @var{line} as if it had been read from the @code{inputrc} file and
  433. perform any key bindings and variable assignments found
  434. (@pxref{Readline Init File}).
  435. @end deftypefun
  436.  
  437. @deftypefun int rl_read_init_file (char *filename)
  438. Read keybindings and variable assignments from @var{filename}
  439. (@pxref{Readline Init File}).
  440. @end deftypefun
  441.  
  442. @node Associating Function Names and Bindings
  443. @subsection Associating Function Names and Bindings
  444.  
  445. These functions allow you to find out what keys invoke named functions
  446. and the functions invoked by a particular key sequence.
  447.  
  448. @deftypefun {Function *} rl_named_function (char *name)
  449. Return the function with name @var{name}.
  450. @end deftypefun
  451.  
  452. @deftypefun {Function *} rl_function_of_keyseq (char *keyseq, Keymap map, int *type)
  453. Return the function invoked by @var{keyseq} in keymap @var{map}.
  454. If @var{map} is NULL, the current keymap is used.  If @var{type} is
  455. not NULL, the type of the object is returned in it (one of @code{ISFUNC},
  456. @code{ISKMAP}, or @code{ISMACR}).
  457. @end deftypefun
  458.  
  459. @deftypefun {char **} rl_invoking_keyseqs (Function *function)
  460. Return an array of strings representing the key sequences used to
  461. invoke @var{function} in the current keymap.
  462. @end deftypefun
  463.  
  464. @deftypefun {char **} rl_invoking_keyseqs_in_map (Function *function, Keymap map)
  465. Return an array of strings representing the key sequences used to
  466. invoke @var{function} in the keymap @var{map}.
  467. @end deftypefun
  468.  
  469. @deftypefun void rl_function_dumper (int readable)
  470. Print the readline function names and the key sequences currently
  471. bound to them to @code{rl_outstream}.  If @var{readable} is non-zero,
  472. the list is formatted in such a way that it can be made part of an
  473. @code{inputrc} file and re-read.
  474. @end deftypefun
  475.  
  476. @deftypefun void rl_list_funmap_names ()
  477. Print the names of all bindable Readline functions to @code{rl_outstream}.
  478. @end deftypefun
  479.  
  480. @node Allowing Undoing
  481. @subsection Allowing Undoing
  482.  
  483. Supporting the undo command is a painless thing, and makes your
  484. functions much more useful.  It is certainly easy to try
  485. something if you know you can undo it.  I could use an undo function for
  486. the stock market.
  487.  
  488. If your function simply inserts text once, or deletes text once, and
  489. uses @code{rl_insert_text ()} or @code{rl_delete_text ()} to do it, then
  490. undoing is already done for you automatically.
  491.  
  492. If you do multiple insertions or multiple deletions, or any combination
  493. of these operations, you should group them together into one operation.
  494. This is done with @code{rl_begin_undo_group ()} and
  495. @code{rl_end_undo_group ()}.
  496.  
  497. The types of events that can be undone are:
  498.  
  499. @example
  500. enum undo_code @{ UNDO_DELETE, UNDO_INSERT, UNDO_BEGIN, UNDO_END @}; 
  501. @end example
  502.  
  503. Notice that @code{UNDO_DELETE} means to insert some text, and
  504. @code{UNDO_INSERT} means to delete some text.  That is, the undo code
  505. tells undo what to undo, not how to undo it.  @code{UNDO_BEGIN} and
  506. @code{UNDO_END} are tags added by @code{rl_begin_undo_group ()} and
  507. @code{rl_end_undo_group ()}.
  508.  
  509. @deftypefun int rl_begin_undo_group ()
  510. Begins saving undo information in a group construct.  The undo
  511. information usually comes from calls to @code{rl_insert_text ()} and
  512. @code{rl_delete_text ()}, but could be the result of calls to
  513. @code{rl_add_undo ()}.
  514. @end deftypefun
  515.  
  516. @deftypefun int rl_end_undo_group ()
  517. Closes the current undo group started with @code{rl_begin_undo_group
  518. ()}.  There should be one call to @code{rl_end_undo_group ()}
  519. for each call to @code{rl_begin_undo_group ()}.
  520. @end deftypefun
  521.  
  522. @deftypefun void rl_add_undo (enum undo_code what, int start, int end, char *text)
  523. Remember how to undo an event (according to @var{what}).  The affected
  524. text runs from @var{start} to @var{end}, and encompasses @var{text}.
  525. @end deftypefun
  526.  
  527. @deftypefun void free_undo_list ()
  528. Free the existing undo list.
  529. @end deftypefun
  530.  
  531. @deftypefun int rl_do_undo ()
  532. Undo the first thing on the undo list.  Returns @code{0} if there was
  533. nothing to undo, non-zero if something was undone.
  534. @end deftypefun
  535.  
  536. Finally, if you neither insert nor delete text, but directly modify the
  537. existing text (e.g., change its case), call @code{rl_modifying ()}
  538. once, just before you modify the text.  You must supply the indices of
  539. the text range that you are going to modify.
  540.  
  541. @deftypefun int rl_modifying (int start, int end)
  542. Tell Readline to save the text between @var{start} and @var{end} as a
  543. single undo unit.  It is assumed that you will subsequently modify
  544. that text.
  545. @end deftypefun
  546.  
  547. @node Redisplay
  548. @subsection Redisplay
  549.  
  550. @deftypefun int rl_redisplay ()
  551. Change what's displayed on the screen to reflect the current contents
  552. of @code{rl_line_buffer}.
  553. @end deftypefun
  554.  
  555. @deftypefun int rl_forced_update_display ()
  556. Force the line to be updated and redisplayed, whether or not
  557. Readline thinks the screen display is correct.
  558. @end deftypefun
  559.  
  560. @deftypefun int rl_on_new_line ()
  561. Tell the update routines that we have moved onto a new (empty) line,
  562. usually after ouputting a newline.
  563. @end deftypefun
  564.  
  565. @deftypefun int rl_reset_line_state ()
  566. Reset the display state to a clean state and redisplay the current line
  567. starting on a new line.
  568. @end deftypefun
  569.  
  570. @deftypefun int rl_message (va_alist)
  571. The arguments are a string as would be supplied to @code{printf}.  The
  572. resulting string is displayed in the @dfn{echo area}.  The echo area
  573. is also used to display numeric arguments and search strings.
  574. @end deftypefun
  575.  
  576. @deftypefun int rl_clear_message ()
  577. Clear the message in the echo area.
  578. @end deftypefun
  579.  
  580. @node Modifying Text
  581. @subsection Modifying Text
  582.  
  583. @deftypefun int rl_insert_text (char *text)
  584. Insert @var{text} into the line at the current cursor position.
  585. @end deftypefun
  586.  
  587. @deftypefun int rl_delete_text (int start, int end)
  588. Delete the text between @var{start} and @var{end} in the current line.
  589. @end deftypefun
  590.  
  591. @deftypefun {char *} rl_copy_text (int start, int end)
  592. Return a copy of the text between @var{start} and @var{end} in
  593. the current line.
  594. @end deftypefun
  595.  
  596. @deftypefun int rl_kill_text (int start, int end)
  597. Copy the text between @var{start} and @var{end} in the current line
  598. to the kill ring, appending or prepending to the last kill if the
  599. last command was a kill command.  The text is deleted.
  600. If @var{start} is less than @var{end},
  601. the text is appended, otherwise prepended.  If the last command was
  602. not a kill, a new kill ring slot is used.
  603. @end deftypefun
  604.  
  605. @node Utility Functions
  606. @subsection Utility Functions
  607.  
  608. @deftypefun int rl_read_key ()
  609. Return the next character available.  This handles input inserted into
  610. the input stream via @var{pending input} (@pxref{Readline Variables})
  611. and @code{rl_stuff_char ()}, macros, and characters read from the keyboard.
  612. @end deftypefun
  613.  
  614. @deftypefun int rl_stuff_char (int c)
  615. Insert @var{c} into the Readline input stream.  It will be "read"
  616. before Readline attempts to read characters from the terminal with
  617. @code{rl_read_key ()}.
  618. @end deftypefun
  619.  
  620. @deftypefun int rl_initialize ()
  621. Initialize or re-initialize Readline's internal state.
  622. @end deftypefun
  623.  
  624. @deftypefun int rl_reset_terminal (char *terminal_name)
  625. Reinitialize Readline's idea of the terminal settings using
  626. @var{terminal_name} as the terminal type (e.g., @code{vt100}).
  627. @end deftypefun
  628.  
  629. @deftypefun int alphabetic (int c)
  630. Return 1 if @var{c} is an alphabetic character.
  631. @end deftypefun
  632.  
  633. @deftypefun int numeric (int c)
  634. Return 1 if @var{c} is a numeric character.
  635. @end deftypefun
  636.  
  637. @deftypefun int ding ()
  638. Ring the terminal bell, obeying the setting of @code{bell-style}.
  639. @end deftypefun
  640.  
  641. The following are implemented as macros, defined in @code{chartypes.h}.
  642.  
  643. @deftypefun int uppercase_p (int c)
  644. Return 1 if @var{c} is an uppercase alphabetic character.
  645. @end deftypefun
  646.  
  647. @deftypefun int lowercase_p (int c)
  648. Return 1 if @var{c} is a lowercase alphabetic character.
  649. @end deftypefun
  650.  
  651. @deftypefun int digit_p (int c)
  652. Return 1 if @var{c} is a numeric character.
  653. @end deftypefun
  654.  
  655. @deftypefun int to_upper (int c)
  656. If @var{c} is a lowercase alphabetic character, return the corresponding
  657. uppercase character.
  658. @end deftypefun
  659.  
  660. @deftypefun int to_lower (int c)
  661. If @var{c} is an uppercase alphabetic character, return the corresponding
  662. lowercase character.
  663. @end deftypefun
  664.  
  665. @deftypefun int digit_value (int c)
  666. If @var{c} is a number, return the value it represents.
  667. @end deftypefun
  668.  
  669. @subsection An Example
  670.  
  671. Here is a function which changes lowercase characters to their uppercase
  672. equivalents, and uppercase characters to lowercase.  If
  673. this function was bound to @samp{M-c}, then typing @samp{M-c} would
  674. change the case of the character under point.  Typing @samp{M-1 0 M-c}
  675. would change the case of the following 10 characters, leaving the cursor on
  676. the last character changed.
  677.  
  678. @example
  679. /* Invert the case of the COUNT following characters. */
  680. int
  681. invert_case_line (count, key)
  682.      int count, key;
  683. @{
  684.   register int start, end, i;
  685.  
  686.   start = rl_point;
  687.  
  688.   if (rl_point >= rl_end)
  689.     return (0);
  690.  
  691.   if (count < 0)
  692.     @{
  693.       direction = -1;
  694.       count = -count;
  695.     @}
  696.   else
  697.     direction = 1;
  698.       
  699.   /* Find the end of the range to modify. */
  700.   end = start + (count * direction);
  701.  
  702.   /* Force it to be within range. */
  703.   if (end > rl_end)
  704.     end = rl_end;
  705.   else if (end < 0)
  706.     end = 0;
  707.  
  708.   if (start == end)
  709.     return (0);
  710.  
  711.   if (start > end)
  712.     @{
  713.       int temp = start;
  714.       start = end;
  715.       end = temp;
  716.     @}
  717.  
  718.   /* Tell readline that we are modifying the line, so it will save
  719.      the undo information. */
  720.   rl_modifying (start, end);
  721.  
  722.   for (i = start; i != end; i++)
  723.     @{
  724.       if (uppercase_p (rl_line_buffer[i]))
  725.         rl_line_buffer[i] = to_lower (rl_line_buffer[i]);
  726.       else if (lowercase_p (rl_line_buffer[i]))
  727.         rl_line_buffer[i] = to_upper (rl_line_buffer[i]);
  728.     @}
  729.   /* Move point to on top of the last character changed. */
  730.   rl_point = (direction == 1) ? end - 1 : start;
  731.   return (0);
  732. @}
  733. @end example
  734.  
  735. @node Custom Completers
  736. @section Custom Completers
  737.  
  738. Typically, a program that reads commands from the user has a way of
  739. disambiguating commands and data.  If your program is one of these, then
  740. it can provide completion for commands, data, or both.
  741. The following sections describe how your program and Readline
  742. cooperate to provide this service.
  743.  
  744. @menu
  745. * How Completing Works::    The logic used to do completion.
  746. * Completion Functions::    Functions provided by Readline.
  747. * Completion Variables::    Variables which control completion.
  748. * A Short Completion Example::    An example of writing completer subroutines.
  749. @end menu
  750.  
  751. @node How Completing Works
  752. @subsection How Completing Works
  753.  
  754. In order to complete some text, the full list of possible completions
  755. must be available.  That is, it is not possible to accurately
  756. expand a partial word without knowing all of the possible words
  757. which make sense in that context.  The Readline library provides
  758. the user interface to completion, and two of the most common
  759. completion functions:  filename and username.  For completing other types
  760. of text, you must write your own completion function.  This section
  761. describes exactly what such functions must do, and provides an example.
  762.  
  763. There are three major functions used to perform completion:
  764.  
  765. @enumerate
  766. @item
  767. The user-interface function @code{rl_complete ()}.  This function is
  768. called with the same arguments as other Readline
  769. functions intended for interactive use:  @var{count} and
  770. @var{invoking_key}.  It isolates the word to be completed and calls
  771. @code{completion_matches ()} to generate a list of possible completions.
  772. It then either lists the possible completions, inserts the possible
  773. completions, or actually performs the
  774. completion, depending on which behavior is desired.
  775.  
  776. @item
  777. The internal function @code{completion_matches ()} uses your
  778. @dfn{generator} function to generate the list of possible matches, and
  779. then returns the array of these matches.  You should place the address
  780. of your generator function in @code{rl_completion_entry_function}.
  781.  
  782. @item
  783. The generator function is called repeatedly from
  784. @code{completion_matches ()}, returning a string each time.  The
  785. arguments to the generator function are @var{text} and @var{state}.
  786. @var{text} is the partial word to be completed.  @var{state} is zero the
  787. first time the function is called, allowing the generator to perform
  788. any necessary initialization, and a positive non-zero integer for
  789. each subsequent call.  When the generator function returns
  790. @code{(char *)NULL} this signals @code{completion_matches ()} that there are
  791. no more possibilities left.  Usually the generator function computes the
  792. list of possible completions when @var{state} is zero, and returns them
  793. one at a time on subsequent calls.  Each string the generator function
  794. returns as a match must be allocated with @code{malloc()}; Readline
  795. frees the strings when it has finished with them.
  796.  
  797. @end enumerate
  798.  
  799. @deftypefun int rl_complete (int ignore, int invoking_key)
  800. Complete the word at or before point.  You have supplied the function
  801. that does the initial simple matching selection algorithm (see
  802. @code{completion_matches ()}).  The default is to do filename completion.
  803. @end deftypefun
  804.  
  805. @deftypevar {Function *} rl_completion_entry_function
  806. This is a pointer to the generator function for @code{completion_matches
  807. ()}.  If the value of @code{rl_completion_entry_function} is
  808. @code{(Function *)NULL} then the default filename generator function,
  809. @code{filename_entry_function ()}, is used.
  810. @end deftypevar
  811.  
  812. @node Completion Functions
  813. @subsection Completion Functions
  814.  
  815. Here is the complete list of callable completion functions present in
  816. Readline.
  817.  
  818. @deftypefun int rl_complete_internal (int what_to_do)
  819. Complete the word at or before point.  @var{what_to_do} says what to do
  820. with the completion.  A value of @samp{?} means list the possible
  821. completions.  @samp{TAB} means do standard completion.  @samp{*} means
  822. insert all of the possible completions.  @samp{!} means to display
  823. all of the possible completions, if there is more than one, as well as
  824. performing partial completion.
  825. @end deftypefun
  826.  
  827. @deftypefun int rl_complete (int ignore, int invoking_key)
  828. Complete the word at or before point.  You have supplied the function
  829. that does the initial simple matching selection algorithm (see
  830. @code{completion_matches ()} and @code{rl_completion_entry_function}).
  831. The default is to do filename
  832. completion.  This calls @code{rl_complete_internal ()} with an
  833. argument depending on @var{invoking_key}.
  834. @end deftypefun
  835.  
  836. @deftypefun int rl_possible_completions (int count, int invoking_key))
  837. List the possible completions.  See description of @code{rl_complete
  838. ()}.  This calls @code{rl_complete_internal ()} with an argument of
  839. @samp{?}.
  840. @end deftypefun
  841.  
  842. @deftypefun int rl_insert_completions (int count, int invoking_key))
  843. Insert the list of possible completions into the line, deleting the
  844. partially-completed word.  See description of @code{rl_complete ()}.
  845. This calls @code{rl_complete_internal ()} with an argument of @samp{*}.
  846. @end deftypefun
  847.  
  848. @deftypefun {char **} completion_matches (char *text, CPFunction *entry_func)
  849. Returns an array of @code{(char *)} which is a list of completions for
  850. @var{text}.  If there are no completions, returns @code{(char **)NULL}.
  851. The first entry in the returned array is the substitution for @var{text}.
  852. The remaining entries are the possible completions.  The array is
  853. terminated with a @code{NULL} pointer.
  854.  
  855. @var{entry_func} is a function of two args, and returns a
  856. @code{(char *)}.  The first argument is @var{text}.  The second is a
  857. state argument; it is zero on the first call, and non-zero on subsequent
  858. calls.  @var{entry_func} returns a @code{NULL}  pointer to the caller
  859. when there are no more matches.
  860. @end deftypefun
  861.  
  862. @deftypefun {char *} filename_completion_function (char *text, int state)
  863. A generator function for filename completion in the general case.  Note
  864. that completion in Bash is a little different because of all
  865. the pathnames that must be followed when looking up completions for a
  866. command.  The Bash source is a useful reference for writing custom
  867. completion functions.
  868. @end deftypefun
  869.  
  870. @deftypefun {char *} username_completion_function (char *text, int state)
  871. A completion generator for usernames.  @var{text} contains a partial
  872. username preceded by a random character (usually @samp{~}).  As with all
  873. completion generators, @var{state} is zero on the first call and non-zero
  874. for subsequent calls.
  875. @end deftypefun
  876.  
  877. @node Completion Variables
  878. @subsection Completion Variables
  879.  
  880. @deftypevar {Function *} rl_completion_entry_function
  881. A pointer to the generator function for @code{completion_matches ()}.
  882. @code{NULL} means to use @code{filename_entry_function ()}, the default
  883. filename completer.
  884. @end deftypevar
  885.  
  886. @deftypevar {CPPFunction *} rl_attempted_completion_function
  887. A pointer to an alternative function to create matches.
  888. The function is called with @var{text}, @var{start}, and @var{end}.
  889. @var{start} and @var{end} are indices in @code{rl_line_buffer} saying
  890. what the boundaries of @var{text} are.  If this function exists and
  891. returns @code{NULL}, or if this variable is set to @code{NULL}, then
  892. @code{rl_complete ()} will call the value of
  893. @code{rl_completion_entry_function} to generate matches, otherwise the
  894. array of strings returned will be used.
  895. @end deftypevar
  896.  
  897. @deftypevar int rl_completion_query_items
  898. Up to this many items will be displayed in response to a
  899. possible-completions call.  After that, we ask the user if she is sure
  900. she wants to see them all.  The default value is 100.
  901. @end deftypevar
  902.  
  903. @deftypevar {char *} rl_basic_word_break_characters
  904. The basic list of characters that signal a break between words for the
  905. completer routine.  The default value of this variable is the characters
  906. which break words for completion in Bash, i.e.,
  907. @code{" \t\n\"\\'`@@$><=;|&@{("}.
  908. @end deftypevar
  909.  
  910. @deftypevar {char *} rl_completer_word_break_characters
  911. The list of characters that signal a break between words for
  912. @code{rl_complete_internal ()}.  The default list is the value of
  913. @code{rl_basic_word_break_characters}.
  914. @end deftypevar
  915.  
  916. @deftypevar {char *} rl_completer_quote_characters
  917. List of characters which can be used to quote a substring of the line.
  918. Completion occurs on the entire substring, and within the substring
  919. @code{rl_completer_word_break_characters} are treated as any other character,
  920. unless they also appear within this list.
  921. @end deftypevar
  922.  
  923. @deftypevar {char *} rl_special_prefixes
  924. The list of characters that are word break characters, but should be
  925. left in @var{text} when it is passed to the completion function.
  926. Programs can use this to help determine what kind of completing to do.
  927. For instance, Bash sets this variable to "$@@" so that it can complete
  928. shell variables and hostnames.
  929. @end deftypevar
  930.  
  931. @deftypevar int rl_ignore_completion_duplicates
  932. If non-zero, then disallow duplicates in the matches.  Default is 1.
  933. @end deftypevar
  934.  
  935. @deftypevar int rl_filename_completion_desired
  936. Non-zero means that the results of the matches are to be treated as
  937. filenames.  This is @emph{always} zero on entry, and can only be changed
  938. within a completion entry generator function.  If it is set to a non-zero
  939. value, directory names have a slash appended and Readline attempts to
  940. quote completed filenames if they contain any embedded word break
  941. characters.
  942. @end deftypevar
  943.  
  944. @deftypevar int rl_filename_quoting_desired
  945. Non-zero means that the results of the matches are to be quoted using
  946. double quotes (or an application-specific quoting mechanism) if the
  947. completed filename contains any characters in
  948. @code{rl_completer_word_break_chars}.  This is @emph{always} non-zero
  949. on entry, and can only be changed within a completion entry generator
  950. function.
  951. @end deftypevar
  952.  
  953. @deftypevar {Function *} rl_ignore_some_completions_function
  954. This function, if defined, is called by the completer when real filename
  955. completion is done, after all the matching names have been generated.
  956. It is passed a @code{NULL} terminated array of matches.
  957. The first element (@code{matches[0]}) is the
  958. maximal substring common to all matches. This function can
  959. re-arrange the list of matches as required, but each element deleted
  960. from the array must be freed.
  961. @end deftypevar
  962.  
  963. @deftypevar {Function *} rl_directory_completion_hook
  964. This function, if defined, is allowed to modify the directory portion
  965. of filenames Readline completes.  It is called with the address of a
  966. string (the current directory name) as an argument.  It could be used
  967. to expand symbolic links or shell variables in pathnames.
  968. @end deftypevar
  969.  
  970. @node A Short Completion Example
  971. @subsection A Short Completion Example
  972.  
  973. Here is a small application demonstrating the use of the GNU Readline
  974. library.  It is called @code{fileman}, and the source code resides in
  975. @file{examples/fileman.c}.  This sample application provides
  976. completion of command names, line editing features, and access to the
  977. history list.
  978.  
  979. @page
  980. @smallexample
  981. /* fileman.c -- A tiny application which demonstrates how to use the
  982.    GNU Readline library.  This application interactively allows users
  983.    to manipulate files and their modes. */
  984.  
  985. #include <stdio.h>
  986. #include <sys/types.h>
  987. #include <sys/file.h>
  988. #include <sys/stat.h>
  989. #include <sys/errno.h>
  990.  
  991. #include <readline/readline.h>
  992. #include <readline/history.h>
  993.  
  994. extern char *getwd ();
  995. extern char *xmalloc ();
  996.  
  997. /* The names of functions that actually do the manipulation. */
  998. int com_list (), com_view (), com_rename (), com_stat (), com_pwd ();
  999. int com_delete (), com_help (), com_cd (), com_quit ();
  1000.  
  1001. /* A structure which contains information on the commands this program
  1002.    can understand. */
  1003.  
  1004. typedef struct @{
  1005.   char *name;            /* User printable name of the function. */
  1006.   Function *func;        /* Function to call to do the job. */
  1007.   char *doc;            /* Documentation for this function.  */
  1008. @} COMMAND;
  1009.  
  1010. COMMAND commands[] = @{
  1011.   @{ "cd", com_cd, "Change to directory DIR" @},
  1012.   @{ "delete", com_delete, "Delete FILE" @},
  1013.   @{ "help", com_help, "Display this text" @},
  1014.   @{ "?", com_help, "Synonym for `help'" @},
  1015.   @{ "list", com_list, "List files in DIR" @},
  1016.   @{ "ls", com_list, "Synonym for `list'" @},
  1017.   @{ "pwd", com_pwd, "Print the current working directory" @},
  1018.   @{ "quit", com_quit, "Quit using Fileman" @},
  1019.   @{ "rename", com_rename, "Rename FILE to NEWNAME" @},
  1020.   @{ "stat", com_stat, "Print out statistics on FILE" @},
  1021.   @{ "view", com_view, "View the contents of FILE" @},
  1022.   @{ (char *)NULL, (Function *)NULL, (char *)NULL @}
  1023. @};
  1024.  
  1025. /* Forward declarations. */
  1026. char *stripwhite ();
  1027. COMMAND *find_command ();
  1028.  
  1029. /* The name of this program, as taken from argv[0]. */
  1030. char *progname;
  1031.  
  1032. /* When non-zero, this global means the user is done using this program. */
  1033. int done;
  1034.  
  1035. char *
  1036. dupstr (s)
  1037.      int s;
  1038. @{
  1039.   char *r;
  1040.  
  1041.   r = xmalloc (strlen (s) + 1);
  1042.   strcpy (r, s);
  1043.   return (r);
  1044. @}
  1045.  
  1046. main (argc, argv)
  1047.      int argc;
  1048.      char **argv;
  1049. @{
  1050.   char *line, *s;
  1051.  
  1052.   progname = argv[0];
  1053.  
  1054.   initialize_readline ();    /* Bind our completer. */
  1055.  
  1056.   /* Loop reading and executing lines until the user quits. */
  1057.   for ( ; done == 0; )
  1058.     @{
  1059.       line = readline ("FileMan: ");
  1060.  
  1061.       if (!line)
  1062.         break;
  1063.  
  1064.       /* Remove leading and trailing whitespace from the line.
  1065.          Then, if there is anything left, add it to the history list
  1066.          and execute it. */
  1067.       s = stripwhite (line);
  1068.  
  1069.       if (*s)
  1070.         @{
  1071.           add_history (s);
  1072.           execute_line (s);
  1073.         @}
  1074.  
  1075.       free (line);
  1076.     @}
  1077.   exit (0);
  1078. @}
  1079.  
  1080. /* Execute a command line. */
  1081. int
  1082. execute_line (line)
  1083.      char *line;
  1084. @{
  1085.   register int i;
  1086.   COMMAND *command;
  1087.   char *word;
  1088.  
  1089.   /* Isolate the command word. */
  1090.   i = 0;
  1091.   while (line[i] && whitespace (line[i]))
  1092.     i++;
  1093.   word = line + i;
  1094.  
  1095.   while (line[i] && !whitespace (line[i]))
  1096.     i++;
  1097.  
  1098.   if (line[i])
  1099.     line[i++] = '\0';
  1100.  
  1101.   command = find_command (word);
  1102.  
  1103.   if (!command)
  1104.     @{
  1105.       fprintf (stderr, "%s: No such command for FileMan.\n", word);
  1106.       return (-1);
  1107.     @}
  1108.  
  1109.   /* Get argument to command, if any. */
  1110.   while (whitespace (line[i]))
  1111.     i++;
  1112.  
  1113.   word = line + i;
  1114.  
  1115.   /* Call the function. */
  1116.   return ((*(command->func)) (word));
  1117. @}
  1118.  
  1119. /* Look up NAME as the name of a command, and return a pointer to that
  1120.    command.  Return a NULL pointer if NAME isn't a command name. */
  1121. COMMAND *
  1122. find_command (name)
  1123.      char *name;
  1124. @{
  1125.   register int i;
  1126.  
  1127.   for (i = 0; commands[i].name; i++)
  1128.     if (strcmp (name, commands[i].name) == 0)
  1129.       return (&commands[i]);
  1130.  
  1131.   return ((COMMAND *)NULL);
  1132. @}
  1133.  
  1134. /* Strip whitespace from the start and end of STRING.  Return a pointer
  1135.    into STRING. */
  1136. char *
  1137. stripwhite (string)
  1138.      char *string;
  1139. @{
  1140.   register char *s, *t;
  1141.  
  1142.   for (s = string; whitespace (*s); s++)
  1143.     ;
  1144.     
  1145.   if (*s == 0)
  1146.     return (s);
  1147.  
  1148.   t = s + strlen (s) - 1;
  1149.   while (t > s && whitespace (*t))
  1150.     t--;
  1151.   *++t = '\0';
  1152.  
  1153.   return s;
  1154. @}
  1155.  
  1156. /* **************************************************************** */
  1157. /*                                                                  */
  1158. /*                  Interface to Readline Completion                */
  1159. /*                                                                  */
  1160. /* **************************************************************** */
  1161.  
  1162. char *command_generator ();
  1163. char **fileman_completion ();
  1164.  
  1165. /* Tell the GNU Readline library how to complete.  We want to try to complete
  1166.    on command names if this is the first word in the line, or on filenames
  1167.    if not. */
  1168. initialize_readline ()
  1169. @{
  1170.   /* Allow conditional parsing of the ~/.inputrc file. */
  1171.   rl_readline_name = "FileMan";
  1172.  
  1173.   /* Tell the completer that we want a crack first. */
  1174.   rl_attempted_completion_function = (CPPFunction *)fileman_completion;
  1175. @}
  1176.  
  1177. /* Attempt to complete on the contents of TEXT.  START and END show the
  1178.    region of TEXT that contains the word to complete.  We can use the
  1179.    entire line in case we want to do some simple parsing.  Return the
  1180.    array of matches, or NULL if there aren't any. */
  1181. char **
  1182. fileman_completion (text, start, end)
  1183.      char *text;
  1184.      int start, end;
  1185. @{
  1186.   char **matches;
  1187.  
  1188.   matches = (char **)NULL;
  1189.  
  1190.   /* If this word is at the start of the line, then it is a command
  1191.      to complete.  Otherwise it is the name of a file in the current
  1192.      directory. */
  1193.   if (start == 0)
  1194.     matches = completion_matches (text, command_generator);
  1195.  
  1196.   return (matches);
  1197. @}
  1198.  
  1199. /* Generator function for command completion.  STATE lets us know whether
  1200.    to start from scratch; without any state (i.e. STATE == 0), then we
  1201.    start at the top of the list. */
  1202. char *
  1203. command_generator (text, state)
  1204.      char *text;
  1205.      int state;
  1206. @{
  1207.   static int list_index, len;
  1208.   char *name;
  1209.  
  1210.   /* If this is a new word to complete, initialize now.  This includes
  1211.      saving the length of TEXT for efficiency, and initializing the index
  1212.      variable to 0. */
  1213.   if (!state)
  1214.     @{
  1215.       list_index = 0;
  1216.       len = strlen (text);
  1217.     @}
  1218.  
  1219.   /* Return the next name which partially matches from the command list. */
  1220.   while (name = commands[list_index].name)
  1221.     @{
  1222.       list_index++;
  1223.  
  1224.       if (strncmp (name, text, len) == 0)
  1225.         return (dupstr(name));
  1226.     @}
  1227.  
  1228.   /* If no names matched, then return NULL. */
  1229.   return ((char *)NULL);
  1230. @}
  1231.  
  1232. /* **************************************************************** */
  1233. /*                                                                  */
  1234. /*                       FileMan Commands                           */
  1235. /*                                                                  */
  1236. /* **************************************************************** */
  1237.  
  1238. /* String to pass to system ().  This is for the LIST, VIEW and RENAME
  1239.    commands. */
  1240. static char syscom[1024];
  1241.  
  1242. /* List the file(s) named in arg. */
  1243. com_list (arg)
  1244.      char *arg;
  1245. @{
  1246.   if (!arg)
  1247.     arg = "";
  1248.  
  1249.   sprintf (syscom, "ls -FClg %s", arg);
  1250.   return (system (syscom));
  1251. @}
  1252.  
  1253. com_view (arg)
  1254.      char *arg;
  1255. @{
  1256.   if (!valid_argument ("view", arg))
  1257.     return 1;
  1258.  
  1259.   sprintf (syscom, "more %s", arg);
  1260.   return (system (syscom));
  1261. @}
  1262.  
  1263. com_rename (arg)
  1264.      char *arg;
  1265. @{
  1266.   too_dangerous ("rename");
  1267.   return (1);
  1268. @}
  1269.  
  1270. com_stat (arg)
  1271.      char *arg;
  1272. @{
  1273.   struct stat finfo;
  1274.  
  1275.   if (!valid_argument ("stat", arg))
  1276.     return (1);
  1277.  
  1278.   if (stat (arg, &finfo) == -1)
  1279.     @{
  1280.       perror (arg);
  1281.       return (1);
  1282.     @}
  1283.  
  1284.   printf ("Statistics for `%s':\n", arg);
  1285.  
  1286.   printf ("%s has %d link%s, and is %d byte%s in length.\n", arg,
  1287.           finfo.st_nlink,
  1288.           (finfo.st_nlink == 1) ? "" : "s",
  1289.           finfo.st_size,
  1290.           (finfo.st_size == 1) ? "" : "s");
  1291.   printf ("Inode Last Change at: %s", ctime (&finfo.st_ctime));
  1292.   printf ("      Last access at: %s", ctime (&finfo.st_atime));
  1293.   printf ("    Last modified at: %s", ctime (&finfo.st_mtime));
  1294.   return (0);
  1295. @}
  1296.  
  1297. com_delete (arg)
  1298.      char *arg;
  1299. @{
  1300.   too_dangerous ("delete");
  1301.   return (1);
  1302. @}
  1303.  
  1304. /* Print out help for ARG, or for all of the commands if ARG is
  1305.    not present. */
  1306. com_help (arg)
  1307.      char *arg;
  1308. @{
  1309.   register int i;
  1310.   int printed = 0;
  1311.  
  1312.   for (i = 0; commands[i].name; i++)
  1313.     @{
  1314.       if (!*arg || (strcmp (arg, commands[i].name) == 0))
  1315.         @{
  1316.           printf ("%s\t\t%s.\n", commands[i].name, commands[i].doc);
  1317.           printed++;
  1318.         @}
  1319.     @}
  1320.  
  1321.   if (!printed)
  1322.     @{
  1323.       printf ("No commands match `%s'.  Possibilties are:\n", arg);
  1324.  
  1325.       for (i = 0; commands[i].name; i++)
  1326.         @{
  1327.           /* Print in six columns. */
  1328.           if (printed == 6)
  1329.             @{
  1330.               printed = 0;
  1331.               printf ("\n");
  1332.             @}
  1333.  
  1334.           printf ("%s\t", commands[i].name);
  1335.           printed++;
  1336.         @}
  1337.  
  1338.       if (printed)
  1339.         printf ("\n");
  1340.     @}
  1341.   return (0);
  1342. @}
  1343.  
  1344. /* Change to the directory ARG. */
  1345. com_cd (arg)
  1346.      char *arg;
  1347. @{
  1348.   if (chdir (arg) == -1)
  1349.     @{
  1350.       perror (arg);
  1351.       return 1;
  1352.     @}
  1353.  
  1354.   com_pwd ("");
  1355.   return (0);
  1356. @}
  1357.  
  1358. /* Print out the current working directory. */
  1359. com_pwd (ignore)
  1360.      char *ignore;
  1361. @{
  1362.   char dir[1024], *s;
  1363.  
  1364.   s = getwd (dir);
  1365.   if (s == 0)
  1366.     @{
  1367.       printf ("Error getting pwd: %s\n", dir);
  1368.       return 1;
  1369.     @}
  1370.  
  1371.   printf ("Current directory is %s\n", dir);
  1372.   return 0;
  1373. @}
  1374.  
  1375. /* The user wishes to quit using this program.  Just set DONE non-zero. */
  1376. com_quit (arg)
  1377.      char *arg;
  1378. @{
  1379.   done = 1;
  1380.   return (0);
  1381. @}
  1382.  
  1383. /* Function which tells you that you can't do this. */
  1384. too_dangerous (caller)
  1385.      char *caller;
  1386. @{
  1387.   fprintf (stderr,
  1388.            "%s: Too dangerous for me to distribute.  Write it yourself.\n",
  1389.            caller);
  1390. @}
  1391.  
  1392. /* Return non-zero if ARG is a valid argument for CALLER, else print
  1393.    an error message and return zero. */
  1394. int
  1395. valid_argument (caller, arg)
  1396.      char *caller, *arg;
  1397. @{
  1398.   if (!arg || !*arg)
  1399.     @{
  1400.       fprintf (stderr, "%s: Argument required.\n", caller);
  1401.       return (0);
  1402.     @}
  1403.  
  1404.   return (1);
  1405. @}
  1406. @end smallexample
  1407.